package com.chuanzhi.demo2;

import java.util.*;

/*
* 2. 分析以下需求，并用代码实现：
	(1)统计每个单词出现的次数
	(2)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
	(3)打印格式：
		to=3
  		think=1
  		you=2
  		//........
* */
public class Count {
    public static void main(String[] args) {
        TreeMap<String , Integer> map = new  TreeMap<>();
        String  stringCount = "If you want to change your fate I think you must come to the dark horse to learn java";
        String[] str = stringCount.split(" ");
        for (String s : str) {
            if(map.containsKey(s)){
                Integer value = map.get(s);
                value++;
                map.put(s,value);
            }else {
                map.put(s,1);
            }
        }

        Set<String> keySet = map.keySet();
        Iterator<String> iterator = keySet.iterator();
        while(iterator.hasNext()){
            String next = iterator.next();
            Integer values = map.get(next);
            System.out.println(next +"-->"+values);
        }
    }
}
